feat(logger): enhance log level handling #1476
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of your changes
This PR introduces the possibility for customers to set the Logger level at runtime as well as retrieving the current level both in string (i.e.
DEBUG
) and numeric (i.e.8
) notations.Before this PR, customers could only set the log level when creating a new Logger instance. There are however use cases (see #1265) in which customers might want to change the log level of an existing Logger instance at runtime. Likewise, customers were also not able to get the current log level as its value was treated as internal by the utility.
The PR introduces two new methods and a public property to the Logger class:
Get current log level
The first method added is
getLevelName()
, this method returns the current log level as string:Get current log level number value
The second change is a new public class property called
level
, which returns the numeric value of the current log level:Set a new log level
The third one is a new
setLogLevel()
method. This method accepts a string (both lower and uppercase) and sets the corresponding log level.If an invalid value is passed, the method will throw an error.
In order to support these methods and streamline the log level handling the PR also changes the Logger class to exclusively use numeric values internally. This, together with the changes above, aligns the Logger utility to the behavior found in the Python version of Powertools for AWS.
Finally, the PR changes the access modifier of the
shouldPrint
method fromprivate
toprotected
, so that customers can optionally extend the method when subclassing the Logger instance and implement custom logging decision logic (#1307 (comment)).The PR also covers a couple of housekeeping items:
Math.random
withrandomInt
fromnode:crypto
in log sampling decision@deprecated
notice tocreateLogger
helper that informs of the function's deprecation in the next major releaseRelated issues, RFCs
Issue number: #1265
Checklist
Breaking change checklist
Is it a breaking change?: NO
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.